<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-23 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	private $business = '';
	
	function __construct($request)
	{
		parent::__construct($request);
		
		$bid = isset($this->request['bid']) ? (int)$this->request['bid'] : '';
		
		if(!$bid)
		{
			$this->messager("请指定商家", 'index.php?m=business');
		}
		
		$info = get_business(array('bid'=>$bid));
			
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("商家不存在", 'index.php?m=business');
		}
		
		$this->business = $info;
		
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$info = $this->business;
		$list = get_business_special_list(array('bid'=>$info['id']));
				
		$info['count'] = max($this->business['special_count'] - count($list), 0);
		
		include template('business_special');
	}
	
	private function add()
	{
		$info = array();
		if(isset($this->request['c']))
		{
			$info = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$info['business_name'] = $this->business['name'];
		$info['name'] = isset($info['name']) ? $info['name'] : '';
		$info['price'] = isset($info['price']) ? $info['price'] : '';
		$info['star_level'] = isset($info['star_level']) ? $info['star_level'] : '';
		$info['desc'] = isset($info['desc']) ? $info['desc'] : '';
		
		$action = 'do_add&bid='.$this->business['id'];
		$action_name = '添加特色菜';
		include template('business_special_info');
	}
	
	private function doAdd()
	{
		$request['uid'] = $this->user['id'];
		$request['bid'] = $this->business['id'];
		$request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$request['price'] = isset($this->request['price']) ? (float)$this->request['price'] : 0;
		$request['star_level'] = isset($this->request['star_level']) ? (float)$this->request['star_level'] : 0;
		
		$request['image'] = '';
		$request['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "index.php?m=business_special&a=add&bid=".$request['bid']."&c=$key";
		
		if(!$request['name'] || !$request['desc'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['desc']) > 250)
		{
			//$this->messager("简介内容超长", $redirect);
			$request['desc'] = cut_string($request['desc'], 250);
		}
				
		if($_FILES['image']['name'])
		{
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$request['image'] = $image_path;
			}
		}
		
		$result = add_business_special($request);
		
		if($result)
		{
			$this->messager("添加成功", 'index.php?m=business_special&a=modify&bid='.$request['bid'].'&id='.$result);
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		$redirect = "index.php?m=business_special&bid=".$this->business['id'];
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}
		
		$result = delete_business_special(array('bid'=>$this->business['id'], 'ids'=>$ids));

		if($result)
		{
			$this->messager("删除成功", $redirect);
		}
		else
		{
			$this->messager("删除失败", $redirect);
		}
	}
	
	private function modify()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'index.php?m=business_special&bid='.$this->business['id']);
		}
		
		if(isset($this->request['c']) && ($info = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$info = get_business_special(array('id'=>$id, 'bid'=>$this->business['id']));
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("特色菜不存在", 'index.php?m=business_special&bid='.$this->business['id']);
			}
		}
		
		$info['business_name'] = $this->business['name'];
		
		$action = 'do_modify&bid='.$this->business['id'].'&id='.$id;
		$action_name = '编辑商家特色菜';
		include template('business_special_info');
	}
	
	private function doModify()
	{
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'index.php?m=business_special&bid='.$this->business['id']);
		}
		
		$info = get_business_special(array('id'=>$id, 'bid'=>$this->business['id']));
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("特色菜不存在", 'index.php?m=business_special&bid='.$this->business['id']);
		}
		
		$request['uid'] = $this->user['id'];
		$request['bid'] = $this->business['id'];
		$request['id'] = $id;
		
		$request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$request['price'] = isset($this->request['price']) ? (float)$this->request['price'] : 0;
		$request['star_level'] = isset($this->request['star_level']) ? (float)$this->request['star_level'] : 0;
		$request['image'] = '';
		
		$request['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "index.php?m=business_special&a=add&bid=".$request['bid']."&c=$key";
		
		if(!$request['name'] || !$request['desc'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['desc']) > 250)
		{
			$this->messager("简介内容超长", $redirect);
		}
		
		$request['uid'] = $this->user['id'];

		if($_FILES['image']['name'])
		{
			$old_image = $info['image'];
			
			if(is_file($old_image))
			{
				delete_image($old_image);
			}
			
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$request['image'] = $image_path;
			}
		}
		else
		{
			$request['image'] = $info['image'];
		}
		
		$result = update_business_special($request);

		if($result)
		{
			$this->messager("修改成功", "index.php?m=business_special&bid=".$request['bid']);
		}
		else
		{
			$this->messager("修改失败", "index.php?m=business_special&bid=".$request['bid']);
		}
	}
}